package com.drguo.higouguo.web;

import java.util.List;

import com.drguo.higouguo.entity.Address;
import com.drguo.higouguo.entity.Order;
import com.drguo.higouguo.entity.OrderDetail;
import com.drguo.higouguo.entity.User;
import com.drguo.higouguo.service.AddressService;
import com.drguo.higouguo.service.GoodsService;
import com.drguo.higouguo.service.OrderService;
import com.opensymphony.xwork2.ActionContext;

public class OrderAction {
	private AddressService addressService;
	private GoodsService goodsService;
	private List<Address> addresses;//用户可能有多个地址
	private List<OrderDetail> orderDetails;
	
	//订单结算
	public String add(){
		String userId = ((User)ActionContext.getContext().getSession().get("user")).getId();
		addresses = addressService.getAddress(userId);//根据用户id获取他的地址
		
		for (OrderDetail orderDetail : orderDetails) {
			orderDetail.setGoods(goodsService.getGoodsById(orderDetail.getGoodsId()));
		}
		
		return "add";
	}
	
	//提交订单
	private Order order;
	private OrderService orderService;
	public String submit(){
		String userId = ((User)ActionContext.getContext().getSession().get("user")).getId();
		order.setUserId(userId);
		orderService.addOrder(order, orderDetails);
		return "submit";
	}
	
	//后台订单列表
	private List<Order> orders;
	public String list(){
		orders=orderService.getOrders();
		return "list";
	}
	
	public String view(){
		order=orderService.getOrderById(order.getId());
		return "view";
	}
	
	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public List<Address> getAddress() {
		return addresses;
	}

	public void setAddress(List<Address> address) {
		this.addresses = address;
	}

	public List<OrderDetail> getOrderDetails() {
		return orderDetails;
	}

	public void setOrderDetails(List<OrderDetail> orderDetails) {
		this.orderDetails = orderDetails;
	}

	public List<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}


	public List<Order> getOrders() {
		return orders;
	}


	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
}
